/*
 * Filename:    CaseCheckServcieImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-20
 */
package com.paic.mhis.hcpms.audit.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.core.auth.biz.service.Authority;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.audit.biz.service.AuditOpinionService;
import com.paic.mhis.hcpms.audit.biz.service.AuditWorkFlowService;
import com.paic.mhis.hcpms.audit.biz.service.ICaseCheckService;
import com.paic.mhis.hcpms.audit.biz.service.ICaseCommonService;
import com.paic.mhis.hcpms.audit.biz.service.RandomCodeService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dao.AuditDispatchDao;
import com.paic.mhis.hcpms.audit.dao.AuditOpinionDao;
import com.paic.mhis.hcpms.audit.dao.AuditProjectDao;
import com.paic.mhis.hcpms.audit.dao.AuditSurveyReviewDao;
import com.paic.mhis.hcpms.audit.dao.CaseCommonDao;
import com.paic.mhis.hcpms.audit.dto.AuditCaseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditCaseReportDTO;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchUserRequestDTO;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchUserResponseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditNotificationDTO;
import com.paic.mhis.hcpms.audit.dto.AuditOpinionDTO;
import com.paic.mhis.hcpms.audit.dto.AuditPointCutTempDTO;
import com.paic.mhis.hcpms.audit.dto.AuditProjectDTO;
import com.paic.mhis.hcpms.audit.dto.AuditReportRespDTO;
import com.paic.mhis.hcpms.audit.dto.AuditTargetDTO;
import com.paic.mhis.hcpms.audit.dto.AuditiCaseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditiStaffInfoDTO;
import com.paic.mhis.hcpms.audit.dto.BatchSubmitResponseDTO;
import com.paic.mhis.hcpms.audit.dto.CaseCheckConfirmRequestDTO;
import com.paic.mhis.hcpms.audit.dto.CaseCheckResponseDTO;
import com.paic.mhis.hcpms.audit.dto.CaseCheckResultDTO;
import com.paic.mhis.hcpms.audit.util.EventCodeUtil;
import com.paic.mhis.hcpms.audit.util.GetFormDateUtil;
import com.paic.mhis.hcpms.common.document.biz.service.IDocumentService;
import com.paic.mhis.hcpms.common.document.dto.DocContentDTO;
import com.paic.mhis.hcpms.common.document.dto.DocParameterCondition;
import com.paic.mhis.hcpms.common.document.dto.DocSaveParamterDTO;
import com.paic.mhis.hcpms.common.document.util.Constant;
import com.paic.mhis.hcpms.common.storage.biz.service.HcpmsStorageService;
import com.paic.mhis.hcpms.common.storage.dto.HcpmsStorageDTO;
import com.paic.mhis.hcpms.common.workflow.vo.HcpmsWorkFlowVO;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityMedicalService;
import com.paic.mock.util.StringUtils;

@Component("caseCheckServcieImpl")
public class CaseCheckServcieImpl implements ICaseCheckService {

	@Autowired
	private AuditOpinionService auditOpinionService;

	@Autowired
	private AuditProjectDao auditProjectDao;

	@Autowired
	private CaseCommonDao caseCommonDao;

	@Autowired
	private AuditOpinionDao auditOpinionDao;

	@Autowired
	private AuditDispatchDao auditDispatchDao;

	@Autowired
	private AuditSurveyReviewDao auditSurveyReviewDao;

	@Autowired
	private AuditWorkFlowService auditWorkFlowService; // 流程服务

	@Autowired
	private RandomCodeService randomCodeService;

	public static final String CASETYPE = "JH";

	@Autowired
	private IDocumentService iDocumentService;

	@Autowired
	private IntegrityMedicalService integrityMedicalService;

	@Autowired
	private CommonDAO commonDAO;

	@Autowired
	private HcpmsStorageService hcpmsStorageService;

	@Autowired
	private ICaseCommonService caseCommonService;

	/**
	 * 根据事件号和案件号加载事件处理，调查结果，稽核项目，稽核结果
	 */
	public CaseCheckResponseDTO loadCaseCheckResponseDTOByCaseCode(
			String caseCode, String problemNo, String username) {
		AuditOpinionDTO opinionProcess = this
				.getProblemProcessOpinion(problemNo); // 事件初审意见
		AuditOpinionDTO opinionAudit = this.getProblemAuditOpinion(problemNo); // 事件审核意见
		AuditOpinionDTO surveyOpinion = this.getSurveyInfoOpionion(problemNo); // 调查详情
		AuditOpinionDTO surveyReview = this.getSurveyReviewOpionion(problemNo);// 调查复核
		AuditOpinionDTO surveyAudit = this.getSurveyAuditiOpionion(problemNo); // 调查审核

		AuditOpinionDTO caseReviewResult = this.getCaseReviewResult(caseCode); // 稽核复核意见

		HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
		tempObject.setBusinessId(caseCode);
		tempObject.setModuleType(AuditConstants.jhModule);
		tempObject.setChildModuleType(AuditConstants.examine);
		tempObject.setBusinessType(AuditConstants.opinionType_13);
		tempObject.setUserId(username);
		Object obj = this.hcpmsStorageService.getStorageDataMap(tempObject);
		if (obj != null) {
			AuditDispatchUserRequestDTO currentObj = JSONObject.toJavaObject(
					((JSONObject) obj), AuditDispatchUserRequestDTO.class);
			if (caseReviewResult != null) {
				caseReviewResult.setOpinionDesc(currentObj
						.getReviewOpinionDesc());
			}

		}

		AuditProjectDTO projectBean = new AuditProjectDTO();
		projectBean.setCaseCode(caseCode);

		List<AuditProjectDTO> auditProjectList = this.auditProjectDao
				.getAuditProjectList(projectBean); // 获取稽核项目

		CaseCheckResultDTO caseCheckResult = this.getCaseCheckResult(caseCode); // 获取稽核初始意见

		AuditOpinionDTO caseAuditResult = this.getCaseAuditiOpionion(caseCode); // 稽核审核

		AuditOpinionDTO caseApproveResult = this
				.getApproveAuditiOpionion(caseCode); // 稽核结论审批意见

		CaseCheckResponseDTO responseBean = new CaseCheckResponseDTO();

		responseBean.setProblemProcessOpinion(opinionProcess);
		responseBean.setProblemAuditOpinion(opinionAudit);
		responseBean.setSurveyResult(surveyOpinion);
		responseBean.setSurveyReview(surveyReview);
		responseBean.setSurveyAudit(surveyAudit);
		responseBean.setAuditProjectList(auditProjectList);
		responseBean.setCaseCheckResult(caseCheckResult);
		responseBean.setCaseReviewResult(caseReviewResult);
		responseBean.setCaseApproveResult(caseApproveResult);
		responseBean.setCaseAuditResult(caseAuditResult);
		return responseBean;
	}

	/**
	 * 根据案件号加载稽核的初始意见
	 * 
	 * @param caseCode
	 * @return
	 */
	private AuditOpinionDTO getCaseReviewResult(String caseCode) {

		AuditOpinionDTO parameter = new AuditOpinionDTO();

		parameter.setProblemNo(caseCode);
		parameter.setTaskType(AuditConstants.taskType_02);
		parameter.setOpinionType(AuditConstants.opinionType_12);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		AuditOpinionDTO returnBean = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		return returnBean;

	}

	/**
	 * 获取事件的处理意见
	 * 
	 * @param problemNo
	 * @return
	 */
	private AuditOpinionDTO getProblemProcessOpinion(String problemNo) {
		AuditOpinionDTO parameter = new AuditOpinionDTO();

		parameter.setProblemNo(problemNo);
		parameter.setTaskType(AuditConstants.taskType_01);
		parameter.setOpinionType(AuditConstants.opinionType_1);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		AuditOpinionDTO returnBean = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		return returnBean;

	}

	/**
	 * 获取事件的审核意见
	 * 
	 * @param problemNo
	 * @return
	 */
	private AuditOpinionDTO getProblemAuditOpinion(String problemNo) {
		AuditOpinionDTO parameter = new AuditOpinionDTO();

		parameter.setProblemNo(problemNo);
		parameter.setTaskType(AuditConstants.taskType_01);
		parameter.setOpinionType(AuditConstants.opinionType_2);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		AuditOpinionDTO returnBean = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		return returnBean;
	}

	/**
	 * 获取调查结果调查调查详情
	 * 
	 * @param problemNo
	 * @return
	 */
	private AuditOpinionDTO getSurveyInfoOpionion(String problemNo) {
		AuditOpinionDTO parameter = new AuditOpinionDTO();

		parameter.setProblemNo(problemNo);
		parameter.setTaskType(AuditConstants.taskType_01);
		parameter.setOpinionType(AuditConstants.opinionType_3);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		AuditOpinionDTO returnBean = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		return returnBean;
	}

	/**
	 * 获取调查结果的 调查复核结果
	 * 
	 * @param problemNo
	 * @return
	 */
	private AuditOpinionDTO getSurveyReviewOpionion(String problemNo) {
		AuditOpinionDTO parameter = new AuditOpinionDTO();

		parameter.setProblemNo(problemNo);
		parameter.setTaskType(AuditConstants.taskType_01);
		parameter.setOpinionType(AuditConstants.opinionType_4);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		AuditOpinionDTO returnBean = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		return returnBean;
	}

	/**
	 * 获取调查结果 中审核意见
	 * 
	 * @param problemNo
	 * @return
	 */
	private AuditOpinionDTO getSurveyAuditiOpionion(String problemNo) {
		AuditOpinionDTO parameter = new AuditOpinionDTO();

		parameter.setProblemNo(problemNo);
		parameter.setTaskType(AuditConstants.taskType_01);
		parameter.setOpinionType(AuditConstants.opinionType_5);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		AuditOpinionDTO returnBean = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		return returnBean;
	}

	/**
	 * 审核意见
	 * 
	 * @param problemNo
	 * @return
	 */
	private AuditOpinionDTO getApproveAuditiOpionion(String caseCode) {
		AuditOpinionDTO parameter = new AuditOpinionDTO();

		parameter.setProblemNo(caseCode);
		parameter.setTaskType(AuditConstants.taskType_02);
		parameter.setOpinionType(AuditConstants.opinionType_15);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		AuditOpinionDTO returnBean = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		return returnBean;
	}

	/**
	 * 审核意见
	 * 
	 * @param problemNo
	 * @return
	 */
	private AuditOpinionDTO getCaseAuditiOpionion(String caseCode) {
		AuditOpinionDTO parameter = new AuditOpinionDTO();

		parameter.setProblemNo(caseCode);
		parameter.setTaskType(AuditConstants.taskType_02);
		parameter.setOpinionType(AuditConstants.opinionType_13);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		AuditOpinionDTO returnBean = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		return returnBean;
	}

	/**
	 * 获取稽核的初始意见
	 * 
	 * @param caseCode
	 * @return
	 */
	private CaseCheckResultDTO getCaseCheckResult(String caseCode) {

		CaseCheckResultDTO caseCheckResultDTO = new CaseCheckResultDTO();

		AuditCaseDTO queryBean = new AuditCaseDTO();
		queryBean.setCaseCode(caseCode);
		AuditCaseDTO caseBean = this.auditProjectDao
				.getAuditCaseDTOByProblemNo(queryBean);

		String auditDate = caseBean.getAuditDate();
		String auditStart = caseBean.getPeriodBeginDate();
		String auditEnd = caseBean.getPeriodEndDate();

		AuditOpinionDTO parameter = new AuditOpinionDTO();

		parameter.setProblemNo(caseCode);
		parameter.setTaskType(AuditConstants.taskType_02);
		parameter.setOpinionType(AuditConstants.opinionType_11);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		AuditOpinionDTO returnBean = this.auditOpinionService
				.queryAuditOpinionDTO(parameter);

		caseCheckResultDTO.setCaseDate(auditDate);
		caseCheckResultDTO.setCaseBeginDate(auditStart);
		caseCheckResultDTO.setCaseEndDate(auditEnd);

		if (returnBean != null) {
			String caseResult = returnBean.getOpinion();
			String caseResultDesc = returnBean.getOpinionDesc();
			String dealUser = returnBean.getDisplayName();
			String dealDate = returnBean.getDealDate();

			caseCheckResultDTO.setCaseResult(caseResult);
			caseCheckResultDTO.setCaseResultDesc(caseResultDesc);
			caseCheckResultDTO.setOpertionDate(dealDate);
			caseCheckResultDTO.setOpertionUser(dealUser);
		}

		return caseCheckResultDTO;

	}

	/**
	 * 审核岗位对于稽核项目的审核逻辑处理1.保存审核意见2.更新案件状态为
	 * 
	 * @throws Exception
	 */

	@Transactional
	public void processCaseCheckAuditConfirm(CaseCheckConfirmRequestDTO bean,
			UserDetails user, JSONObject transferJson) throws Exception {

		integrityMedicalService.addIntegrityPointTemps(transferJson,
				user.getUsername());// 临时扣分
		this.caseCommonService.deleteAuditCasePoint(bean.getCaseCode(),
				user.getUsername()); // 删除临时表的扣分

		// 删除临时表的信息
		this.deleteCaseAuditTempData(user.getUsername(), bean.getCaseCode());

		String firstOpinion = bean.getAuditFirstOpinion(); // 稽核初审岗位意见
		String secondOpinion = bean.getAuditSecondOpinion(); // 稽核复核岗位意见

		String opinion = bean.getAuditOpinion();

		this.updateAuditOpinino(bean.getReviewOpinionDesc(), bean.getCaseCode());

		if (AuditConstants.opinionResult_1.equals(firstOpinion)
				&& AuditConstants.opinionResult_1.equals(secondOpinion)
				&& AuditConstants.opinionResult_1.equals(opinion)) { // 稽核初审正常
																		// 复核岗位同意
																		// 稽核审核岗位也同意了
																		// 则案件结束了
			this.caseOverProcess(bean, user);

		} else if (AuditConstants.opinionResult_0.equals(firstOpinion)
				&& AuditConstants.opinionResult_1.equals(secondOpinion)
				&& AuditConstants.opinionResult_1.equals(opinion)) { // 核初审异常
																		// 复核岗位同意
																		// 稽核审核岗位也同意了
			this.processAgress(bean, user); // 则案件需要稽核

		} else {
			// 其他情况都是重新稽核也就是重新分配到岗位给用户重新去调查

			this.processDisAgress(bean, user);
		}

	}

	/**
	 * 删除案件审核的临时数据
	 * 
	 * @param username
	 * @param taskCode
	 */
	@Transactional
	private void deleteCaseAuditTempData(String username, String caseCode) {
		HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
		tempObject.setBusinessId(caseCode);
		tempObject.setModuleType(AuditConstants.jhModule);
		tempObject.setChildModuleType(AuditConstants.examine);
		tempObject.setBusinessType(AuditConstants.opinionType_13);
		tempObject.setUserId(username);
		this.hcpmsStorageService.deleteStorageData(tempObject);
	}

	/**
	 * 案件结束了，
	 * 
	 * @param bean
	 * @param user
	 * @throws Exception
	 */
	@Transactional
	private void caseOverProcess(CaseCheckConfirmRequestDTO bean,
			UserDetails user) throws Exception {

		String caseCode = bean.getCaseCode();

		// 1.,更新审核的状态为结束
		AuditCaseDTO caseBean = new AuditCaseDTO();
		caseBean.setCaseCode(bean.getCaseCode());
		caseBean.setCaseState(AuditConstants.auditCaseStatus_7);
		caseBean.setUpdatedBy(user.getUsername());
		caseBean.setTempFlag(AuditConstants.TEMP_FLAG_0);
		this.auditDispatchDao.updateAuditCaseDTO(caseBean);

		// 2.保存审核意见
		Date date = new Date();
		String dateStr = GetFormDateUtil.getStrFromDate(date);

		AuditOpinionDTO opionionBean = new AuditOpinionDTO();
		opionionBean.setOpinionType(AuditConstants.opinionType_13);
		opionionBean.setProblemNo(bean.getCaseCode());
		opionionBean.setTaskType(AuditConstants.taskType_02);
		opionionBean.setOpinion(AuditConstants.opinionResult_1);
		opionionBean.setOpinionDesc(bean.getAuditOpinionDesc());
		opionionBean.setDealUserName(user.getUsername());
		opionionBean.setDealDate(dateStr);

		List<AuditOpinionDTO> list = this.auditOpinionDao
				.queryAuditOpinionDTOInfo(opionionBean);
		if (list != null && list.size() > 0) {
			this.auditOpinionDao.updateAuditOpinionDTOInfo(opionionBean);
		} else {
			opionionBean.setIsDeleted(AuditConstants.isDeleted);
			this.auditOpinionDao.addOpinion(opionionBean);
		}

		// 结束当前的案件流程
		Map<String, String> condition = new HashMap<String, String>();
		condition.put(AuditConstants.AUDIT_CASE_CHECK_TYPE,
				AuditConstants.AUDIT_CASE_CHECK_TYPE_03);
		this.auditWorkFlowService.processWorkFlow(caseCode, user, condition,
				AuditConstants.TASK_TYPE_CASE);

	}

	/**
	 * 处理稽核也就是下发通知函到医院
	 * 
	 * @param bean
	 * @throws Exception
	 */
	@Transactional
	private void processAgress(CaseCheckConfirmRequestDTO bean, UserDetails user)
			throws Exception {
		String opinionDesc = bean.getAuditOpinionDesc();
		String opertionUsername = bean.getOpertionUsername();
		String caseCode = bean.getCaseCode();

		Date date = new Date();
		String dateStr = GetFormDateUtil.getStrFromDate(date);

		// 增加意见
		AuditOpinionDTO opionionBean = new AuditOpinionDTO();

		opionionBean.setOpinionType(AuditConstants.opinionType_13);
		opionionBean.setProblemNo(bean.getCaseCode());
		opionionBean.setTaskType(AuditConstants.taskType_02);
		opionionBean.setOpinion(AuditConstants.opinionResult_1);
		opionionBean.setOpinionDesc(opinionDesc);
		opionionBean.setDealUserName(opertionUsername);
		opionionBean.setDealDate(dateStr);

		List<AuditOpinionDTO> list = this.auditOpinionDao
				.queryAuditOpinionDTOInfo(opionionBean);
		if (list != null && list.size() > 0) {
			this.auditOpinionDao.updateAuditOpinionDTOInfo(opionionBean);
		} else {
			opionionBean.setIsDeleted(AuditConstants.isDeleted);
			this.auditOpinionDao.addOpinion(opionionBean);
		}

		// 更新案件状态
		// Date deadLineDate = new Date();

		// String noticDateStr = AuditConstants.getStrFromDate(deadLineDate);

		AuditCaseDTO caseBean = new AuditCaseDTO();
		caseBean.setCaseCode(bean.getCaseCode());
		caseBean.setCaseState(AuditConstants.auditCaseStatus_6);
		caseBean.setUpdatedBy(opertionUsername);
		caseBean.setTempFlag(AuditConstants.TEMP_FLAG_0);

		// caseBean.setNotifyDate(noticDateStr);
		// caseBean.setAffirmDeadline(bean.getAffirmDeadline());
		// caseBean.setNotifyDoc(noticDateStr);

		this.auditDispatchDao.updateAuditCaseDTO(caseBean);

		// 处理流程 处理下发函
		Map<String, String> condition = new HashMap<String, String>();
		condition.put(AuditConstants.AUDIT_CASE_CHECK_TYPE,
				AuditConstants.AUDIT_CASE_CHECK_TYPE_01);
		this.auditWorkFlowService.processWorkFlow(caseCode, user, condition,
				AuditConstants.TASK_TYPE_CASE);

	}

	/**
	 * 处理重新稽核(分配人员重新调查)
	 * 
	 * 1.案件审核重新分配
	 * 
	 * (1).如果第一次分配的人员和案件重新分配的人员是一样的,逻辑处理如下：
	 * 
	 * 则将当前案件状态修改成案件待稽核,扭转流程和保存意见
	 * 
	 * (2)如果第二次分配的案件稽核岗和第一次的一样，而案件复核不一样,则
	 * 
	 * 将当前案件结束掉同时,拷贝一份案件信息状态为案件待稽核,将案件要稽核的对象拷贝一份，然后将老的案件稽核对象虚拟删除。
	 * 
	 * 将老案件的意见类型是稽核待分配和稽核结论拷贝一份到新的案件意见中，同时将老的案件意见类型是稽核待分配和稽核结论和稽核结论复核都虚拟删除掉.
	 * 保存一条老案件的意见类型是审核的且状态是删除的，拷贝这条意见到新的案件意见中状态是审核的且未删除
	 * 
	 * 同时将该分配的人员更新到新的意见类型是稽核待分配中的APPOINT_FIRST和APPOINT_SECOND中
	 * 
	 * (2)如果第二次分配的案件稽核结论复核和第一次一样,而稽核结论不一样，则
	 * 
	 * 将当前案件结束掉同时,拷贝一份案件信息状态为案件待稽核,将案件要稽核的对象拷贝一份，然后将老的案件稽核对象虚拟删除。
	 * 
	 * 将老案件的意见类型是稽核待分配和稽核结论复核拷贝一份到新的案件意见中，同时将老的案件意见类型是稽核待分配和稽核结论和稽核结论复核都虚拟删除掉.
	 * 保存一条老案件的意见类型是稽核结论审核的且状态是删除的，拷贝这条意见到新的案件意见中状态是稽核结论审核的且未删除
	 * 
	 * 同时将该分配的人员更新到新的意见类型是稽核待分配中的APPOINT_FIRST和APPOINT_SECOND中
	 * 
	 * (3) 如果第二次分配的人员和第一次完全不一样,则
	 * 将当前案件结束掉同时,拷贝一份案件信息状态为案件待稽核,将案件要稽核的对象拷贝一份，然后将老的案件稽核对象虚拟删除。
	 * 将老的案件意见类型是稽核待分配和稽核结论和稽核结论复核都虚拟删除掉,拷贝一份意见类型是稽核待分配的给新的案件
	 * 同时将该分配的人员更新到新的意见类型是稽核待分配中的APPOINT_FIRST和APPOINT_SECOND中
	 * 将此次的稽核结论审核意见保存起来，且一个是老的案件意见，一个是新的案件意见,老的要删除标志
	 * 
	 * 以上情况后都要走流程扭转
	 * 
	 * 
	 * 
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	@Transactional
	private void processDisAgress(CaseCheckConfirmRequestDTO bean,
			UserDetails user) throws Exception {
		String caseCode = bean.getCaseCode();
		// 保存当前的意见
		Date dealDate = new Date();
		String dealDateStr = GetFormDateUtil.getStrFromDate(dealDate);

		AuditOpinionDTO problemOpinion = new AuditOpinionDTO();
		problemOpinion.setProblemNo(caseCode);
		problemOpinion.setTaskType(AuditConstants.taskType_02);
		problemOpinion.setOpinion(bean.getAuditOpinion());
		problemOpinion.setOpinionDesc(bean.getAuditOpinionDesc());
		problemOpinion.setOpinionType(AuditConstants.opinionType_13);
		problemOpinion.setDealDate(dealDateStr);
		problemOpinion.setDealUserName(user.getUsername());
		problemOpinion.setCreatedBy(user.getUsername());

		// 根据当前的事件号去加载3-调查结论4-调查结论复核这两个意见类型
		AuditOpinionDTO parameter = new AuditOpinionDTO();
		parameter.setProblemNo(caseCode);
		parameter.setTaskType(AuditConstants.taskType_02);

		// A岗位稽核结论原始意见
		parameter.setOpinionType(AuditConstants.opinionType_11);
		AuditOpinionDTO surveyOpinonA = this.auditOpinionDao
				.queryOpionionDTO(parameter);

		// B岗位稽核结论复核原始意见
		parameter.setOpinionType(AuditConstants.opinionType_12);
		AuditOpinionDTO surveyOpinonB = this.auditOpinionDao
				.queryOpionionDTO(parameter);

		String dealAUsername = surveyOpinonA.getDealUserName();
		String dealBUsername = surveyOpinonB.getDealUserName();

		String userACode = bean.getUserAName();
		String userBCode = bean.getUserBName();

		if (userACode.equals(dealAUsername) && userBCode.equals(dealBUsername)) { // 两次分配都一样

			this.processDispatchSame(problemOpinion, bean, user);

		} else { // 都不相同
			this.processASameandBDifferent(problemOpinion, bean, user);
		}

	}

	/**
	 * 根据案件号加载对应的稽核项目,然后删除老的，拷贝一份出来
	 * 
	 * @param caseCode
	 */
	@Transactional
	private void processCaseProject(String caseCode, String newCaseCode,
			UserDetails user) {

		AuditProjectDTO parameter = new AuditProjectDTO();
		parameter.setCaseCode(caseCode);
		parameter.setIsDeleted(AuditConstants.isDeleted);
		List<AuditProjectDTO> list = this.auditProjectDao
				.queryAuditProjectDTO(parameter);

		if (list != null && list.size() > 0) {

			for (AuditProjectDTO bean : list) {

				AuditProjectDTO newProjectBean = new AuditProjectDTO();
				BeanUtils.copyProperties(bean, newProjectBean);
				newProjectBean.setCaseCode(newCaseCode);

				this.auditProjectDao.insertAuditProject(newProjectBean);

				// 更新当前的稽核项目为删除
				bean.setIsDeleted(AuditConstants.isDeleted_1);
				bean.setUpdatedBy(user.getUsername());
				bean.setId("'" + bean.getId() + "'");
				this.auditProjectDao.updateAuditProject(bean);

			}

		}

	}

	@Transactional
	private void processASameandBDifferent(AuditOpinionDTO problemOpinion,
			CaseCheckConfirmRequestDTO bean, UserDetails user) throws Exception {
		// 获取新稽核编号
		String newCaseCode = this.randomCodeService.getCode(
				EventCodeUtil.event_codeJH, EventCodeUtil.industry_type2);
		if (org.apache.commons.lang.StringUtils.isEmpty(newCaseCode)) {
			newCaseCode = this.randomCodeService.getCode(
					EventCodeUtil.event_codeJH, EventCodeUtil.industry_type2);
		}
		String caseCode = bean.getCaseCode();

		AuditOpinionDTO parameter = new AuditOpinionDTO();
		parameter.setProblemNo(caseCode);
		parameter.setTaskType(AuditConstants.taskType_02);

		// A岗位稽核结论原始意见
		parameter.setOpinionType(AuditConstants.opinionType_11);
		AuditOpinionDTO surveyOpinonA = this.auditOpinionDao
				.queryOpionionDTO(parameter);

		// B岗位稽核结论复核原始意见
		parameter.setOpinionType(AuditConstants.opinionType_12);
		AuditOpinionDTO surveyOpinonB = this.auditOpinionDao
				.queryOpionionDTO(parameter);

		// 案件稽核分配意见
		parameter.setOpinionType(AuditConstants.opinionType_10);
		AuditOpinionDTO surveyFirstOpinion = this.auditOpinionDao
				.queryOpionionDTO(parameter);

		String dealAUsername = surveyOpinonA.getDealUserName();
		String dealBUsername = surveyOpinonB.getDealUserName();

		String userACode = bean.getUserAName();
		String userBCode = bean.getUserBName();

		// 结束当前案件流程,并拷贝案件到新案件中，拷贝案件稽核对象到新的案件稽核对象中
		AuditCaseDTO caseBean = new AuditCaseDTO();
		caseBean.setCaseCode(caseCode);
		caseBean.setCaseState(AuditConstants.auditCaseStatus_7);// 案件已结束
		caseBean.setIsDeleted(AuditConstants.isDeleted_1);
		caseBean.setRollbackFlag(AuditConstants.rollBackFlag_0);
		caseBean.setTempFlag(AuditConstants.TEMP_FLAG_0);

		// 重新生成一个案件信息
		AuditiCaseDTO queryCaseBean = new AuditiCaseDTO();
		queryCaseBean.setCaseCode(caseCode);
		AuditiCaseDTO returnBean = this.auditSurveyReviewDao
				.loadAuditiCaseDTOByCode(queryCaseBean);

		AuditiCaseDTO newAuditiCaseBean = new AuditiCaseDTO();
		BeanUtils.copyProperties(returnBean, newAuditiCaseBean);
		newAuditiCaseBean.setCaseCode(newCaseCode);
		newAuditiCaseBean.setCaseStatus(AuditConstants.auditCaseStatus_2);
		newAuditiCaseBean.setIsDeleted(AuditConstants.isDeleted);
		newAuditiCaseBean.setTempFlag(AuditConstants.TEMP_FLAG_0);
		newAuditiCaseBean.setRollbackFlag(AuditConstants.rollBackFlag_1);
		newAuditiCaseBean.setComplaintFlag("");
		newAuditiCaseBean.setAffirmDate(null);

		AuditPointCutTempDTO pointCondition = new AuditPointCutTempDTO();
		pointCondition.setCaseType(AuditConstants.case_type_02);
		pointCondition.setCaseNo(caseCode);
		pointCondition.setIsValid(AuditConstants.isDeleted);

		List<AuditPointCutTempDTO> pointList = this.caseCommonDao
				.loadAuditPointCutTempDTO(pointCondition);
		if (pointList != null && pointList.size() > 0) {
			for (AuditPointCutTempDTO currentBean : pointList) {

				AuditPointCutTempDTO newPointBean = new AuditPointCutTempDTO();

				BeanUtils.copyProperties(currentBean, newPointBean);
				newPointBean.setCaseNo(newCaseCode);
				newPointBean.setIsValid(AuditConstants.isDeleted);
				this.caseCommonDao.saveAuditPointCutTempDTO(newPointBean);
			}
		}

		// 更新老的案件信息
		this.auditProjectDao.updateAuditCaseInfo(caseBean);
		// 拷贝案件稽核对象
		this.getCopyCace(newCaseCode, caseCode);

		if (userACode.equals(dealAUsername) && !userBCode.equals(dealBUsername)) {
			this.getDistribution(newCaseCode, caseCode, user,
					newAuditiCaseBean, surveyOpinonA, surveyOpinonB,
					surveyFirstOpinion, userACode, userBCode);
		} else if (userBCode.equals(dealBUsername)
				&& !userACode.equals(dealAUsername)) {
			this.getDistribution(newCaseCode, newAuditiCaseBean, surveyOpinonA,
					surveyOpinonB, surveyFirstOpinion, userACode, userBCode);
		} else {
			this.getAuditInfo(newCaseCode, newAuditiCaseBean, surveyOpinonA,
					surveyOpinonB, surveyFirstOpinion, userACode, userBCode);
		}
		// 处理审核意见 老的
		problemOpinion.setProblemNo(caseCode);

		List<AuditOpinionDTO> queryOpinionList = this.auditOpinionDao
				.queryAuditOpinionDTOInfo(problemOpinion);
		if (queryOpinionList != null && queryOpinionList.size() > 0) {
			problemOpinion.setIsDeleted(AuditConstants.isDeleted_1);
			this.auditOpinionDao.updateAuditOpinionDTOInfo(problemOpinion);
		} else {
			problemOpinion.setIsDeleted(AuditConstants.isDeleted_1);
			this.auditOpinionDao.addOpinion(problemOpinion);
		}
		// 处理审核意见新的
		this.dealReviewOpinion(userACode, userBCode, dealAUsername,
				dealBUsername, problemOpinion, newCaseCode);

		// 处理逆转流程
		this.getWorkFlow(newCaseCode, userACode, user);
	}

	// 处理逆转流程
	public void getWorkFlow(String newCaseCode, String userACode,
			UserDetails user) throws Exception {
		HcpmsWorkFlowVO wfvo = new HcpmsWorkFlowVO();
		List<Authority> authorityList = user.getAuthorities();
		List<String> role = new ArrayList<String>();
		for (Authority authority : authorityList) {
			role.add(authority.getAuthority());
		}
		wfvo.setRole(role);
		wfvo.setTaskNo(newCaseCode);
		wfvo.setTaskType(AuditConstants.TASK_TYPE_CASE);
		wfvo.setActCurrent(user.getUsername());
		wfvo.setAssigner(userACode);
		this.auditWorkFlowService.startWorkFlow(wfvo);
	}

	// 处理审核意见新的
	public void dealReviewOpinion(String userACode, String userBCode,
			String dealAUsername, String dealBUsername,
			AuditOpinionDTO problemOpinion, String newCaseCode) {
		if (userBCode.equals(dealBUsername) || userACode.equals(dealAUsername)) {
			problemOpinion.setProblemNo(newCaseCode);
			List<AuditOpinionDTO> currentOpinionList = this.auditOpinionDao
					.queryAuditOpinionDTOInfo(problemOpinion);

			if (currentOpinionList != null && currentOpinionList.size() > 0) {
				problemOpinion.setIsDeleted(AuditConstants.isDeleted);
				this.auditOpinionDao.updateAuditOpinionDTOInfo(problemOpinion);
			} else {
				problemOpinion.setIsDeleted(AuditConstants.isDeleted);
				this.auditOpinionDao.addOpinion(problemOpinion);
			}
		}
	}

	// 拷贝案件稽核对象
	public void getCopyCace(String newCaseCode, String caseCode) {
		// 拷贝案件稽核对象
		AuditTargetDTO targetParamter = new AuditTargetDTO();
		targetParamter.setTaskCode(caseCode);
		targetParamter.setTaskType(AuditConstants.taskType_02);
		List<AuditTargetDTO> list = this.auditSurveyReviewDao
				.queryAuditTargetDTO(targetParamter);

		if (list != null && list.size() > 0) {
			for (AuditTargetDTO currentTarget : list) {

				AuditTargetDTO newTargetBean = new AuditTargetDTO();
				BeanUtils.copyProperties(currentTarget, newTargetBean);
				newTargetBean.setTaskCode(newCaseCode);
				// 保存新的稽核对象
				this.auditSurveyReviewDao.insertAuditTargetDTO(newTargetBean);
				// 将老的稽核对象虚拟删除掉
				currentTarget.setIsDeleted(AuditConstants.isDeleted_1);
				this.auditSurveyReviewDao.updateAuditTargetInfo(currentTarget);

			}
		}
	}

	// 两次分配A岗位相同，B不同
	public void getDistribution(String newCaseCode, String caseCode,
			UserDetails user, AuditiCaseDTO newAuditiCaseBean,
			AuditOpinionDTO surveyOpinonA, AuditOpinionDTO surveyOpinonB,
			AuditOpinionDTO surveyFirstOpinion, String userACode,
			String userBCode) {
		// 拷贝稽核的项目
		this.processCaseProject(caseCode, newCaseCode, user);
		// 保存新的案件信息
		this.auditSurveyReviewDao.saveAuditCaseDTO(newAuditiCaseBean);

		AuditOpinionDTO newAOpinion = new AuditOpinionDTO();
		BeanUtils.copyProperties(surveyOpinonA, newAOpinion);
		newAOpinion.setProblemNo(newCaseCode);
		this.auditOpinionDao.addOpinion(newAOpinion);
		surveyOpinonA.setIsDeleted(AuditConstants.isDeleted_1);
		this.auditOpinionDao.updateAuditOpinionDTOInfo(surveyOpinonA);

		AuditOpinionDTO newFirstOpinion = new AuditOpinionDTO();
		BeanUtils.copyProperties(surveyFirstOpinion, newFirstOpinion);
		newFirstOpinion.setAppointFirst(userACode);
		newFirstOpinion.setAppointSecond(userBCode);
		newFirstOpinion.setProblemNo(newCaseCode);
		newFirstOpinion.setIsDeleted(AuditConstants.isDeleted);
		this.auditOpinionDao.addOpinion(newFirstOpinion);
		surveyFirstOpinion.setIsDeleted(AuditConstants.isDeleted_1);
		this.auditOpinionDao.updateAuditOpinionDTOInfo(surveyFirstOpinion);

		surveyOpinonB.setIsDeleted(AuditConstants.isDeleted_1);
		this.auditOpinionDao.updateAuditOpinionDTOInfo(surveyOpinonB);
	}

	// 两次分配B相同,A岗位不同
	public void getDistribution(String newCaseCode,
			AuditiCaseDTO newAuditiCaseBean, AuditOpinionDTO surveyOpinonA,
			AuditOpinionDTO surveyOpinonB, AuditOpinionDTO surveyFirstOpinion,
			String userACode, String userBCode) {
		// 处理稽核日期稽核摘要等信息
		this.processAuditCaseInfo(newAuditiCaseBean);
		// 保存新的案件信息
		this.auditSurveyReviewDao.saveAuditCaseDTO(newAuditiCaseBean);

		AuditOpinionDTO newBOpinion = new AuditOpinionDTO();
		BeanUtils.copyProperties(surveyOpinonB, newBOpinion);
		newBOpinion.setProblemNo(newCaseCode);
		newBOpinion.setIsDeleted(AuditConstants.isDeleted);
		this.auditOpinionDao.addOpinion(newBOpinion);

		surveyOpinonB.setIsDeleted(AuditConstants.isDeleted_1);
		this.auditOpinionDao.updateAuditOpinionDTOInfo(surveyOpinonB);

		AuditOpinionDTO newFirstOpinion = new AuditOpinionDTO();
		BeanUtils.copyProperties(surveyFirstOpinion, newFirstOpinion);
		newFirstOpinion.setAppointFirst(userACode);
		newFirstOpinion.setAppointSecond(userBCode);
		newFirstOpinion.setProblemNo(newCaseCode);
		newFirstOpinion.setIsDeleted(AuditConstants.isDeleted);
		this.auditOpinionDao.addOpinion(newFirstOpinion);

		surveyFirstOpinion.setIsDeleted(AuditConstants.isDeleted_1);
		this.auditOpinionDao.updateAuditOpinionDTOInfo(surveyFirstOpinion);

		surveyOpinonA.setIsDeleted(AuditConstants.isDeleted_1);
		this.auditOpinionDao.updateAuditOpinionDTOInfo(surveyOpinonA);
	}

	// 处理稽核日期稽核摘要等信息
	public void getAuditInfo(String newCaseCode,
			AuditiCaseDTO newAuditiCaseBean, AuditOpinionDTO surveyOpinonA,
			AuditOpinionDTO surveyOpinonB, AuditOpinionDTO surveyFirstOpinion,
			String userACode, String userBCode) {
		// 处理稽核日期稽核摘要等信息
		this.processAuditCaseInfo(newAuditiCaseBean);
		// 保存新的案件信息
		this.auditSurveyReviewDao.saveAuditCaseDTO(newAuditiCaseBean);

		surveyOpinonA.setIsDeleted(AuditConstants.isDeleted_1);
		surveyOpinonB.setIsDeleted(AuditConstants.isDeleted_1);

		this.auditOpinionDao.updateAuditOpinionDTOInfo(surveyOpinonA);
		this.auditOpinionDao.updateAuditOpinionDTOInfo(surveyOpinonB);

		AuditOpinionDTO newFirstOpinion = new AuditOpinionDTO();
		BeanUtils.copyProperties(surveyFirstOpinion, newFirstOpinion);
		newFirstOpinion.setAppointFirst(userACode);
		newFirstOpinion.setAppointSecond(userBCode);
		newFirstOpinion.setProblemNo(newCaseCode);
		newFirstOpinion.setIsDeleted(AuditConstants.isDeleted);
		this.auditOpinionDao.addOpinion(newFirstOpinion);

		surveyFirstOpinion.setIsDeleted(AuditConstants.isDeleted_1);
		this.auditOpinionDao.updateAuditOpinionDTOInfo(surveyFirstOpinion);
	}

	/**
	 * 审核岗位审核案件信息的时候,进行退回案件的时候，需要把稽核岗位的所填写的稽核日期，稽核时段，不合理费用，稽核摘要清空掉
	 * 
	 * @param caseBean
	 */
	private void processAuditCaseInfo(AuditiCaseDTO caseBean) {
		caseBean.setAuditDate(null);
		caseBean.setAuditSummary("");
		caseBean.setPeriodBeginDate(null);
		caseBean.setPeriodEndDate(null);
		caseBean.setTotalDeduction(null);
	}

	/**
	 * 处理两次分配都一样的逻辑
	 * 
	 * @param problemOpinion
	 * @param bean
	 * @param user
	 * @throws Exception
	 */
	@Transactional
	private void processDispatchSame(AuditOpinionDTO problemOpinion,
			CaseCheckConfirmRequestDTO bean, UserDetails user) throws Exception {
		String caseCode = bean.getCaseCode();
		AuditCaseDTO caseBean = new AuditCaseDTO();
		caseBean.setCaseCode(caseCode);
		caseBean.setCaseState(AuditConstants.auditCaseStatus_2);// 案件待稽核
		caseBean.setRollbackFlag(AuditConstants.rollBackFlag_1);
		caseBean.setTempFlag(AuditConstants.TEMP_FLAG_0);
		this.auditProjectDao.updateAuditCaseDTOByProblemNo(caseBean);
		// 先判断当前意见是否存在,如果存在了更新
		this.checkOpinion(problemOpinion);
		// 扭转历史流程
		Map<String, String> condition = new HashMap<String, String>();
		condition.put(AuditConstants.AUDIT_CASE_CHECK_TYPE,
				AuditConstants.AUDIT_CASE_CHECK_TYPE_02);
		this.auditWorkFlowService.processWorkFlow(caseCode, user,
				bean.getUserAName(), condition, AuditConstants.TASK_TYPE_CASE);

	}

	@Transactional
	private void checkOpinion(AuditOpinionDTO problemOpinion) {
		List<AuditOpinionDTO> queryOpinionList = this.auditOpinionDao
				.queryAuditOpinionDTOInfo(problemOpinion);
		if (queryOpinionList != null && queryOpinionList.size() > 0) {
			this.auditOpinionDao.updateAuditOpinionDTOInfo(problemOpinion);
		} else {
			problemOpinion.setIsDeleted(AuditConstants.isDeleted);
			this.auditOpinionDao.addOpinion(problemOpinion);
		}
	}

	/**
	 * 判断当前的意见是否存在，如果存在了就更新，否则保存
	 * 
	 * @param opinion
	 */
	@Transactional
	private void processOpinionInfo(AuditOpinionDTO opinion) {
		List<AuditOpinionDTO> opinionList = this.auditOpinionDao
				.queryAuditOpinionDTOInfo(opinion);
		if (opinionList != null && opinionList.size() > 0) {
			this.auditOpinionDao.updateAuditOpinionDTOInfo(opinion);
		} else {
			this.auditOpinionDao.addOpinion(opinion);
		}

	}

	@Transactional
	private void updateAuditOpinino(String reviewOpinionDesc, String caseCode) {
		AuditOpinionDTO opinion = new AuditOpinionDTO();
		opinion.setTaskType(AuditConstants.taskType_02);
		opinion.setProblemNo(caseCode);
		opinion.setOpinionType(AuditConstants.opinionType_12);
		opinion.setOpinionDesc(reviewOpinionDesc);
		this.auditOpinionDao.updateAuditOpinionDTOInfo(opinion);
	}

	/**
	 * 处理暂存的逻辑
	 */
	@Transactional
	@Override
	public AuditDispatchUserResponseDTO processTempSave(
			AuditDispatchUserRequestDTO requestBean, String type,
			JSONObject transferJson, String userName) throws Exception {

		AuditDispatchUserResponseDTO responseBean = new AuditDispatchUserResponseDTO();
		String caseCode = requestBean.getProblemNo();

		if ("save".equals(type)) {
			HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
			tempObject.setBusinessId(caseCode);
			tempObject.setModuleType(AuditConstants.jhModule);
			tempObject.setChildModuleType(AuditConstants.examine);
			tempObject.setBusinessType(AuditConstants.opinionType_13);
			tempObject.setUserId(userName);
			tempObject.setDataMap(requestBean);
			this.hcpmsStorageService.saveStorageData(tempObject, false);
			// 暂存扣分
			this.caseCommonService.saveAuditCasePoint(caseCode, transferJson,
					userName);
		}

		if ("load".equals(type)) {
			HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
			tempObject.setBusinessId(caseCode);
			tempObject.setModuleType(AuditConstants.jhModule);
			tempObject.setChildModuleType(AuditConstants.examine);
			tempObject.setBusinessType(AuditConstants.opinionType_13);
			tempObject.setUserId(userName);
			Object obj = this.hcpmsStorageService.getStorageDataMap(tempObject);
			if (obj != null) {
				AuditDispatchUserRequestDTO currentObj = JSONObject
						.toJavaObject(((JSONObject) obj),
								AuditDispatchUserRequestDTO.class);
				AuditOpinionDTO loadOpinion = new AuditOpinionDTO();
				loadOpinion.setOpinion(currentObj.getOpinion());
				loadOpinion.setOpinionDesc(currentObj.getOpinionDesc());
				responseBean.setAuditOpinion(loadOpinion);

			}

		}
		return responseBean;
	}

	/**
	 * 根据案件编码加载对应的稽核报告和稽核告知函信息
	 */
	@Override
	public AuditReportRespDTO loadReportInfo(String caseCode) {
		AuditReportRespDTO responseBean = new AuditReportRespDTO();

		// 重新生成一个案件信息
		AuditiCaseDTO queryCaseBean = new AuditiCaseDTO();
		queryCaseBean.setCaseCode(caseCode);
		AuditiCaseDTO returnBean = this.auditSurveyReviewDao
				.loadAuditiCaseDTOByCode(queryCaseBean);

		// A岗位稽核结论原始意见
		AuditOpinionDTO parameter = new AuditOpinionDTO();
		parameter.setProblemNo(caseCode);
		parameter.setTaskType(AuditConstants.taskType_02);// A岗位稽核结论原始意见
		parameter.setOpinionType(AuditConstants.opinionType_11);
		AuditOpinionDTO surveyOpinonA = this.auditOpinionDao
				.queryOpionionDTO(parameter);

		AuditCaseReportDTO reportDTO = new AuditCaseReportDTO();

		Date auditDate = returnBean.getAuditDate(); // 稽核日期
		String auditSummary = returnBean.getAuditSummary(); // 稽核摘要

		Date startDate = returnBean.getPeriodBeginDate();
		Date endDate = returnBean.getPeriodEndDate();

		String auditDateStr = GetFormDateUtil.getStrFromDate(auditDate);
		String startDateStr = GetFormDateUtil.getStrFromDateByModal(
				"yyyy-MM-dd", startDate);
		String endDateStr = GetFormDateUtil.getStrFromDateByModal("yyyy-MM-dd",
				endDate);

		String auditFormatStart = GetFormDateUtil.getStrFromDateByModal(
				"yyyyMMdd", startDate);
		String auditFormatEnd = GetFormDateUtil.getStrFromDateByModal(
				"yyyyMMdd", endDate);

		reportDTO.setAuditDate(auditDateStr);
		reportDTO.setAuditSummary(auditSummary);
		reportDTO.setProcessOpinion(surveyOpinonA.getOpinionDesc());
		reportDTO.setAuditStart(startDateStr);
		reportDTO.setAuditEnd(endDateStr);
		reportDTO.setAuditRang(startDateStr + "至" + endDateStr);
		reportDTO.setCaseCode(caseCode);
		reportDTO.setAuditFormatStart(auditFormatStart);
		reportDTO.setAuditFormatEnd(auditFormatEnd);
		setProcessOpinion(caseCode, reportDTO, false);
		// /处理文档
		DocParameterCondition condition = new DocParameterCondition();
		condition.setDocType(Constant.docType_05);
		condition.setTaskType(Constant.taskType_05);
		condition.setTaskNo(caseCode);

		List<DocContentDTO> list = this.iDocumentService
				.queryByDocParameterCondition(condition);
		AuditNotificationDTO notificationBean = new AuditNotificationDTO();
		this.processDocumentNotification(notificationBean, list);

		responseBean.setReportBean(reportDTO);
		responseBean.setTellReportBean(notificationBean);
		return responseBean;
	}

	private void processDocumentNotification(
			AuditNotificationDTO notificationBean, List<DocContentDTO> list) {

		for (DocContentDTO bean : list) {
			String docKey = bean.getDocKey();
			String docValue1 = bean.getDocValue1();

			if (AuditConstants.opinionDesc.equals(docKey)) {
				notificationBean.setOpinionDesc(docValue1);
			} else if (AuditConstants.auditDate.equals(docKey)) {
				notificationBean.setAuditDate(docValue1);
			} else if (AuditConstants.fullAddress.equals(docKey)) {
				notificationBean.setFullAddress(docValue1);
			} else if (AuditConstants.healthcareProviderName.equals(docKey)) {
				notificationBean.setHealthcareProviderName(docValue1);
			} else if (AuditConstants.periodEndDate.equals(docKey)) {
				String currentValue = GetFormDateUtil.getStrFromStrByFormat(
						docValue1, "yyyy-MM-dd", "yyyyMMdd");
				notificationBean.setPeriodEndDate(currentValue);
			} else if (AuditConstants.periodBeginDate.equals(docKey)) {
				String currentValue = GetFormDateUtil.getStrFromStrByFormat(
						docValue1, "yyyy-MM-dd", "yyyyMMdd");
				notificationBean.setPeriodBeginDate(currentValue);
			} else if (AuditConstants.caseCode.equals(docKey)) {
				notificationBean.setCaseCode(docValue1);
			} else if (AuditConstants.lealPerson.equals(docKey)) {
				notificationBean.setLealPerson(docValue1);
			} else if (AuditConstants.auditSummary.equals(docKey)) {
				notificationBean.setAuditSummary(docValue1);
			} else if (AuditConstants.pointStartDate.equals(docKey)) {
				notificationBean.setPointStartDate(docValue1);
			} else if (AuditConstants.sealDate.equals(docKey)) {
				notificationBean.setSealDate(docValue1);
			} else if (AuditConstants.regionName.equals(docKey)) {
				notificationBean.setRegionName(docValue1);
			} else if (AuditConstants.scoreInfo.equals(docKey)) {
				notificationBean.setScoreInfo(docValue1);
			}
		}
	}

	/**
	 * 审核岗对案件进行审核撤销操作
	 * 
	 * 将当前案件状态从稽核待审批到稽核待审核,
	 * 
	 * @throws Exception
	 */
	@Transactional
	@Override
	public void auditCancelProcess(String caseCode, UserDetails user)
			throws Exception {
		AuditCaseDTO caseBean = new AuditCaseDTO();
		caseBean.setCaseCode(caseCode);
		caseBean.setCaseState(AuditConstants.auditCaseStatus_4);
		this.auditDispatchDao.updateAuditCaseDTO(caseBean);

		// 处理流程 处理下发函
		Map<String, String> condition = new HashMap<String, String>();
		condition.put(AuditConstants.AUDIT_CASE_APPROVE_TYPE,
				AuditConstants.AUDIT_CASE_APPROVE_TYPE_03);
		this.auditWorkFlowService.processWorkFlow(caseCode, user, condition,
				AuditConstants.TASK_TYPE_CASE);
	}

	/**
	 * 更新稽核报告和稽核告知函的信息
	 */
	@Transactional
	@Override
	public void updateAuditReportInfoAndNotification(
			AuditCaseReportDTO reportBean, AuditNotificationDTO tellReportBean) {

		/**
		 * 更新稽核报告
		 */
		if (reportBean != null) {

			AuditCaseDTO caseBean = new AuditCaseDTO();
			caseBean.setCaseCode(reportBean.getCaseCode());
			caseBean.setAuditSummary(reportBean.getAuditSummary());
			caseBean.setPeriodBeginDate(reportBean.getAuditStart());
			caseBean.setPeriodEndDate(reportBean.getAuditEnd());
			caseBean.setAuditDate(reportBean.getAuditDate());
			this.auditDispatchDao.updateAuditCaseDTO(caseBean);

			AuditOpinionDTO parameter = new AuditOpinionDTO();
			parameter.setProblemNo(reportBean.getCaseCode());
			parameter.setTaskType(AuditConstants.taskType_02);// A岗位稽核结论原始意见
			parameter.setOpinionType(AuditConstants.opinionType_11);
			AuditOpinionDTO surveyOpinonA = this.auditOpinionDao
					.queryOpionionDTO(parameter);

			String id = surveyOpinonA.getId();
			AuditOpinionDTO updateOpinion = new AuditOpinionDTO();
			updateOpinion.setOpinionDesc(reportBean.getProcessOpinion());
			updateOpinion.setId(id);
			this.auditOpinionDao.updateAuditOpinionDTOInfo(updateOpinion);
		}

		/**
		 * 更新稽核告知函信息
		 */
		if (tellReportBean != null) {
			AuditCaseDTO caseBean = new AuditCaseDTO();
			caseBean.setCaseCode(tellReportBean.getCaseCode());
			caseBean.setAuditSummary(tellReportBean.getAuditSummary());
			this.auditDispatchDao.updateAuditCaseDTO(caseBean);

			AuditOpinionDTO parameter = new AuditOpinionDTO();
			parameter.setProblemNo(tellReportBean.getCaseCode());
			parameter.setTaskType(AuditConstants.taskType_02);// A岗位稽核结论原始意见
			parameter.setOpinionType(AuditConstants.opinionType_11);
			AuditOpinionDTO surveyOpinonA = this.auditOpinionDao
					.queryOpionionDTO(parameter);

			String id = surveyOpinonA.getId();
			AuditOpinionDTO updateOpinion = new AuditOpinionDTO();
			updateOpinion.setOpinionDesc(tellReportBean.getOpinionDesc());
			updateOpinion.setId(id);
			this.auditOpinionDao.updateAuditOpinionDTOInfo(updateOpinion);

			DocSaveParamterDTO docParamter = new DocSaveParamterDTO();

			docParamter.setDocType(Constant.docType_05);
			docParamter.setTaskNo(tellReportBean.getCaseCode());
			docParamter.setTaskType(Constant.taskType_05);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put(AuditConstants.opinionDesc, tellReportBean.getOpinionDesc());
			map.put(AuditConstants.auditSummary,
					tellReportBean.getAuditSummary());
			map.put(AuditConstants.sealDate, tellReportBean.getSealDate());

			docParamter.setMap(map);
			this.iDocumentService.updateDocContentDTO(docParamter);

		}
	}

	@Override
	public AuditNotificationDTO getAuditNotificationDTO(String caseCode) {
		// /处理文档
		DocParameterCondition condition = new DocParameterCondition();
		condition.setDocType(Constant.docType_05);
		condition.setTaskType(Constant.taskType_05);
		condition.setTaskNo(caseCode);

		List<DocContentDTO> list = this.iDocumentService
				.queryByDocParameterCondition(condition);
		AuditNotificationDTO notificationBean = new AuditNotificationDTO();
		this.processDocumentNotification(notificationBean, list);

		return notificationBean;
	}

	@Override
	public AuditCaseReportDTO getAuditCaseReportDTOByCaseCode(String caseCode) {
		// 重新生成一个案件信息
		AuditiCaseDTO queryCaseBean = new AuditiCaseDTO();
		queryCaseBean.setCaseCode(caseCode);
		AuditiCaseDTO returnBean = this.auditSurveyReviewDao
				.loadAuditiCaseDTOByCode(queryCaseBean);

		// A岗位稽核结论原始意见
		AuditOpinionDTO parameter = new AuditOpinionDTO();
		parameter.setProblemNo(caseCode);
		parameter.setTaskType(AuditConstants.taskType_02);// A岗位稽核结论原始意见
		parameter.setOpinionType(AuditConstants.opinionType_11);
		AuditOpinionDTO surveyOpinonA = this.auditOpinionDao
				.queryOpionionDTO(parameter);

		AuditCaseReportDTO reportDTO = new AuditCaseReportDTO();

		Date auditDate = returnBean.getAuditDate(); // 稽核日期
		String auditSummary = returnBean.getAuditSummary(); // 稽核摘要

		Date startDate = returnBean.getPeriodBeginDate();
		Date endDate = returnBean.getPeriodEndDate();

		String auditDateStr = GetFormDateUtil.getStrFromDate(auditDate);
		String startDateStr = GetFormDateUtil.getStrFromDateByModal(
				"yyyy-MM-dd", startDate);
		String endDateStr = GetFormDateUtil.getStrFromDateByModal("yyyy-MM-dd",
				endDate);

		String auditFormatStart = GetFormDateUtil.getStrFromDateByModal(
				"yyyyMMdd", startDate);
		String auditFormatEnd = GetFormDateUtil.getStrFromDateByModal(
				"yyyyMMdd", endDate);

		reportDTO.setAuditDate(auditDateStr);
		reportDTO.setAuditSummary(auditSummary);
		reportDTO.setProcessOpinion(surveyOpinonA.getOpinionDesc());
		reportDTO.setAuditStart(startDateStr);
		reportDTO.setAuditEnd(endDateStr);
		reportDTO.setAuditRang(auditFormatStart + "-" + auditFormatEnd);
		reportDTO.setCaseCode(caseCode);
		reportDTO.setAuditFormatStart(auditFormatStart);
		reportDTO.setAuditFormatEnd(auditFormatEnd);
		setProcessOpinion(caseCode, reportDTO, true);

		// 获取统筹区
		String regionName = (String) commonDAO.get(
				"caseCommon.queryRegionFromNotice", caseCode);

		reportDTO.setRegionName(regionName);

		return reportDTO;
	}

	/**
	 * 处理稽核审核的批量提交业务逻辑
	 * 
	 * @throws Exception
	 */
	@Transactional
	@Override
	public BatchSubmitResponseDTO processBatchSubmitInfo(String[] caseCodeList,
			UserDetails user, String opinion, String opinionDesc)
			throws Exception {
		List<String> agressCaseCode = new ArrayList<String>(); // 保留案件稽核是走同意的路线
		List<String> disAgressList = new ArrayList<String>(); // 保留案件稽核是走重新稽核的路线
		BatchSubmitResponseDTO messsageBean = new BatchSubmitResponseDTO();

		if (caseCodeList != null && caseCodeList.length > 0) {
			this.getReviewOpinino(caseCodeList, agressCaseCode, disAgressList);

			if (StringUtils.hasText(opinion)
					&& AuditConstants.opinionResult_1.equals(opinion)) { // 同意
				if (agressCaseCode != null && agressCaseCode.size() > 0) {
					this.getWork(agressCaseCode, disAgressList, user, opinion,
							opinionDesc, messsageBean);
				} else {
					messsageBean.setFlag(true);
					messsageBean.setMessage("操作成功0条,失败" + caseCodeList.length
							+ "条!");
				}
			} else if (StringUtils.hasText(opinion)
					&& AuditConstants.opinionResult_0.equals(opinion)) {
				this.getAuditReview(caseCodeList, agressCaseCode,
						disAgressList, user, opinion, opinionDesc, messsageBean);
			}
		}
		return messsageBean;

	}

	public void getReviewOpinino(String[] caseCodeList,
			List<String> agressCaseCode, List<String> disAgressList) {
		for (String caseCode : caseCodeList) {
			AuditOpinionDTO reviewOpinino = this
					.getReviewBOpinonByCaseCode(caseCode); // 复核的意见

			if (reviewOpinino != null
					&& reviewOpinino.getOpinion().equals(
							AuditConstants.opinionResult_1)) { // 同意
				agressCaseCode.add(caseCode);
			} else if (reviewOpinino != null
					&& reviewOpinino.getOpinion().equals(
							AuditConstants.opinionResult_0)) { // 保留意见
				disAgressList.add(caseCode);
			}
		}
	}

	public void getWork(List<String> agressCaseCode,
			List<String> disAgressList, UserDetails user, String opinion,
			String opinionDesc, BatchSubmitResponseDTO messsageBean)
			throws Exception {
		for (String currentCaseCode : agressCaseCode) {
			AuditCaseDTO dto = new AuditCaseDTO();
			dto.setCaseCode(currentCaseCode);
			AuditCaseDTO rsl = (AuditCaseDTO) commonDAO.get(
					"audticase.getCaseState", dto);
			if (rsl.getCaseState().equals(AuditConstants.auditCaseStatus_6)) {
				continue;
			}

			CaseCheckConfirmRequestDTO bean = new CaseCheckConfirmRequestDTO();
			bean.setCaseCode(currentCaseCode);
			bean.setOpertionUsername(user.getUsername());
			bean.setAuditOpinion(opinion);
			bean.setAuditOpinionDesc(opinionDesc);
			this.processAgress(bean, user);
			// 删除当前案件的临时表信息
			this.deleteBatchSubmitTempInfo(currentCaseCode);
			// 处理临时表的暂存医务人员和参保的信息
			this.caseCommonService.batchOpertionMedicalCommon(currentCaseCode,
					AuditConstants.taskType_02, user.getUsername());
			// 处理扣分的逻辑处理
			// ,先从临时表中查询出该案件的临时扣分信息，然后转一到正式的临时表信息，然后删除临时表的扣分信息

			JSONObject transferJson = this.caseCommonService
					.queryBatchOpertionAuditCasePoint(currentCaseCode,
							user.getUsername());
			this.integrityMedicalService.addIntegrityPointTemps(transferJson,
					user.getUsername());
			this.caseCommonService.deleteAuditCasePoint(currentCaseCode,
					user.getUsername());
		}
		messsageBean.setFlag(true);
		messsageBean.setMessage("操作成功" + agressCaseCode.size() + "条,失败"
				+ disAgressList.size() + "条!");
	}

	// 重新稽核
	public void getAuditReview(String[] caseCodeList,
			List<String> agressCaseCode, List<String> disAgressList,
			UserDetails user, String opinion, String opinionDesc,
			BatchSubmitResponseDTO messsageBean) throws Exception {
		// 重新稽核
		if (disAgressList != null && disAgressList.size() > 0) {
			for (String currentCaseCode : disAgressList) {
				AuditCaseDTO dto = new AuditCaseDTO();
				dto.setCaseCode(currentCaseCode);
				AuditCaseDTO rsl = (AuditCaseDTO) commonDAO.get(
						"audticase.getCaseState", dto);
				if (!rsl.getCaseState()
						.equals(AuditConstants.auditCaseStatus_5)) {
					continue;
				}

				AuditOpinionDTO parameter = new AuditOpinionDTO();
				parameter.setProblemNo(currentCaseCode);
				parameter.setTaskType(AuditConstants.taskType_02);
				// 案件稽核分配意见
				parameter.setOpinionType(AuditConstants.opinionType_10);
				AuditOpinionDTO surveyFirstOpinion = this.auditOpinionDao
						.queryOpionionDTO(parameter);

				CaseCheckConfirmRequestDTO bean = new CaseCheckConfirmRequestDTO();
				bean.setCaseCode(currentCaseCode);
				bean.setOpertionUsername(user.getUsername());
				bean.setAuditOpinion(opinion);
				bean.setAuditOpinionDesc(opinionDesc);
				bean.setUserAName(surveyFirstOpinion.getAppointFirst());
				bean.setUserBName(surveyFirstOpinion.getAppointSecond());
				this.processDisAgress(bean, user);
				this.deleteBatchSubmitTempInfo(currentCaseCode);

				// 处理临时表的暂存医务人员和参保的信息
				this.caseCommonService.batchOpertionMedicalCommon(
						currentCaseCode, AuditConstants.taskType_02,
						user.getUsername());
				// 处理扣分的逻辑处理
				// ,先从临时表中查询出该案件的临时扣分信息，然后转一到正式的临时表信息，然后删除临时表的扣分信息
				JSONObject transferJson = this.caseCommonService
						.queryBatchOpertionAuditCasePoint(currentCaseCode,
								user.getUsername());
				this.integrityMedicalService.addIntegrityPointTemps(
						transferJson, user.getUsername());
				this.caseCommonService.deleteAuditCasePoint(currentCaseCode,
						user.getUsername());
			}
			messsageBean.setFlag(true);
			messsageBean.setMessage("操作成功" + disAgressList.size() + "条,失败"
					+ agressCaseCode.size() + "条!");
		} else {
			messsageBean.setFlag(true);
			messsageBean.setMessage("操作成功0条,失败" + caseCodeList.length + "条!");
		}
	}

	/**
	 * 根据案件编码加载该案件的复核的意见信息
	 * 
	 * @param caseCode
	 */
	private AuditOpinionDTO getReviewBOpinonByCaseCode(String caseCode) {
		AuditOpinionDTO parameter = new AuditOpinionDTO();
		parameter.setProblemNo(caseCode);
		parameter.setTaskType(AuditConstants.taskType_02);

		// B岗位稽核结论复核原始意见
		parameter.setOpinionType(AuditConstants.opinionType_12);
		AuditOpinionDTO surveyOpinonB = this.auditOpinionDao
				.queryOpionionDTO(parameter);
		return surveyOpinonB;
	}

	/**
	 * 做批量提交的时候，删除临时表的记录数据
	 * 
	 * @param caseCodes
	 */
	@Transactional
	private void deleteBatchSubmitTempInfo(String code) {
		HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
		tempObject.setModuleType(AuditConstants.jhModule);
		tempObject.setChildModuleType(AuditConstants.examine);
		tempObject.setBusinessType(AuditConstants.opinionType_13);
		tempObject.setBusinessId(code);
		this.hcpmsStorageService.deleteStorageData(tempObject);

	}

	/**
	 * 
	 * @param caseCode
	 * @param reportDTO
	 * @param siplemScore
	 */
	@SuppressWarnings("unchecked")
	private void setProcessOpinion(String caseCode,
			AuditCaseReportDTO reportDTO, boolean forExport) {
		List<AuditiStaffInfoDTO> asi = commonDAO.query(
				"caseCommon.queryStaffListWithPoint", caseCode);
		if (asi.size() == 0)
			return;
		StringBuffer sb = new StringBuffer();
		sb.append("本次稽核医保服务人员诚信计分情况为：\n");
		for (AuditiStaffInfoDTO to : asi) {
			if (to.getPoint() == null)
				to.setPoint("0");
			sb.append(to.getName() + "   ").append(to.getDepartName() + "   ")
					.append(to.getPersonType() + "   ")
					.append("本次计分 -" + to.getPoint().trim() + "分 ；\n");
		}
		reportDTO.setScoreInfo(sb.toString());
		if (forExport) {
			reportDTO.setProcessOpinion(reportDTO.getProcessOpinion() + "\n"
					+ sb.toString());
		}
	}
}
